package com.umeox.watch.moto.gallery.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.umeox.watch.moto.gallery.R;
import com.umeox.watch.moto.gallery.bean.Photo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zl on 2018/7/2.
 */

public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.ItemViewHolder> {

    private Context mContext;
    private List<Photo> mPhotos = new ArrayList<>();

    private List<Photo> mPhotosSelected = new ArrayList<>();

    private ItemClickListener itemClickListener;
    private ItemLongClickListener itemLongClickListener;


    public AlbumAdapter(Context context, ItemClickListener itemClickListener, ItemLongClickListener itemLongClickListener) {
        this.mContext = context;
        this.itemClickListener = itemClickListener;
        this.itemLongClickListener = itemLongClickListener;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_album_list, parent, false);
        return new ItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {

        final Photo photo = mPhotos.get(position);
        Glide.with(mContext)
                .load(photo.getPath())
                .apply(RequestOptions.overrideOf(158).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL))
                .into(holder.iv);
        holder.mView.setVisibility(photo.isSelected() ? View.VISIBLE : View.GONE);
        holder.itemView.setOnClickListener(v -> itemClickListener.onItemClicked(position));
        holder.itemView.setOnLongClickListener(v -> itemLongClickListener.onItemLongClick(position));
    }

    @Override
    public int getItemCount() {
        return mPhotos.size();
    }

    public void setDatas(ArrayList<Photo> photos) {
        mPhotos.clear();
        mPhotos.addAll(photos);
        notifyDataSetChanged();
    }

    public void addOrRemoveSelected(int position) {
        final Photo photo = mPhotos.get(position);
        final boolean selected = !photo.isSelected();

        if (selected) {
            mPhotosSelected.add(photo);
        } else {
            mPhotosSelected.remove(photo);
            if (mPhotosSelected.isEmpty()) {
                itemClickListener.notifyPhotoSelectedIsEmpty();
            }
        }
        photo.setSelected(selected);
    }

    public ArrayList<String> getPhotoPaths() {
        ArrayList<String> strings = new ArrayList<>();
        for (Photo photo : mPhotosSelected) {
            strings.add(photo.getPath());
        }
        return strings;
    }

    public void resetData() {
        for (Photo photo : mPhotosSelected) {
            final int position = mPhotos.indexOf(photo);
            if (position >= 0){
                mPhotos.get(position).setSelected(false);
                notifyItemChanged(mPhotos.indexOf(photo));
            }
        }
        mPhotosSelected.clear();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        private final ImageView iv;
        private final ImageView mView;

        public ItemViewHolder(View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.iv_photo);
            mView = itemView.findViewById(R.id.iv_add);
        }
    }

    public interface ItemClickListener {
        void onItemClicked(int position);

        void notifyPhotoSelectedIsEmpty();
    }

    public interface ItemLongClickListener {
        boolean onItemLongClick(int position);
    }

    public List<Photo> getPhotosSelected() {
        return mPhotosSelected;
    }

}

